﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using QMERP.Domain.Dtos;
using QMERP.Domain.Entities;
using QMERP.Infrastructure.EFCore;
using QMERP.Infrastructure.EFCore.Extensions;
using QMERP.Infrastructure.Extensions;
using QMERP.Infrastructure.Security;
using System.Data;

namespace QMERP.Application.Services
{
    public class StockActionLogService : IStockActionLogService
    {
        private readonly IMapper _mapper;
        private readonly QMERPDbContext _db;
        public StockActionLogService(IMapper mapper, QMERPDbContext db)
        {
            _mapper = mapper;
            _db = db;
        }
        /// <summary>
        /// 出入库记录窗体
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<StockActionLogResponse>> Query(PageRequest<StockActionLogRequest> request)
        {
            var response = new PageResponse<StockActionLogResponse>();
            var query = Where(request);
            var list = await query.Paging(request.PageIndex, request.PageSize).ToListAsync();
            response.TotalCount = await query.CountAsync();
            response.PageIndex = request.PageIndex;
            response.PageSize = request.PageSize;
            response.List = _mapper.Map<List<StockActionLogResponse>>(list);
            return response;
        }
        /// <summary>
        /// 获取id的出入库记录详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<StockActionLogResponse> Detail(long id)
        {
            var model = await _db.StockActionLog
                .Include(x => x.StockActionLogDetails)
                .Include(x => x.StockActionLogDetails).Include(x => x.Warehouse)
                .Include(x => x.StockActionLogDetails).Include(x => x.Lens).AsNoTracking().FindByIdAsync(id);
            var response = _mapper.Map<StockActionLogResponse>(model);
            return response;
        }
        /// <summary>
        /// 获取出入库记录主要信息列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<StockActionLogMainInfoResponse>> GetStockActionLogMainInfos(PageRequest<StockActionLogRequest> request)
        {
            var list = await Where(request).ToListAsync();
            return _mapper.Map<List<StockActionLogMainInfoResponse>>(list);
        }
        private IQueryable<StockActionLog> Where(PageRequest<StockActionLogRequest> request)
        {
            IQueryable<StockActionLog> query = _db.StockActionLog
                .Include(x => x.StockActionLogDetails)
                .Include(x => x.Warehouse)
                .Include(x => x.Lens).AsNoTracking();

            if (!request.Query.StockActionType.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.StockActionType.Contains(request.Query.StockActionType!));
            }
            if (!request.Query.BusinessOrderNumber.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.BusinessOrderNumber!.Contains(request.Query.BusinessOrderNumber!));
            }
            if (!request.Query.BusinessType.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.BusinessType == request.Query.BusinessType);
            }
            if (request.Query.WarehouseId != null)
            {
                query = query.Where(x => x.WarehouseId ==request.Query.WarehouseId.Value);
            }
            if (request.Query.LensId != null)
            {
                query = query.Where(x => x.LensId == request.Query.LensId.Value);
            }
            if (!request.Query.CreatedByName.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.CreatedByName.Contains(request.Query.CreatedByName!));
            }
            if (request.Query.CreatedOnStart != null)
            {
                query = query.Where(x => x.CreatedOn >= request.Query.CreatedOnStart.Value.ToStart());
            }
            if (request.Query.CreatedOnEnd != null)
            {
                query = query.Where(x => x.CreatedOn <= request.Query.CreatedOnEnd.Value.ToEnd());
            }
            //if (request.Query.Status != null)
            //{
            //    if (request.Query.Status == StockActionLogStatusEnum.Pending.ToString())
            //    {
            //        query = query.OrderByDescending(x => x.CreatedOn);
            //    }
            //    else if (request.Query.Status == StockActionLogStatusEnum.Checked.ToString())
            //    {
            //        query = query.OrderByDescending(x => x.CheckedOn);
            //    }
            //    else if (request.Query.Status == StockActionLogStatusEnum.Finnish.ToString())
            //    {
            //        query = query.OrderByDescending(x => x.FinishOn);
            //    }
            //    else
            //    {
            //        query = query.OrderByDescending(x => x.UpdatedOn);
            //    }
            //}
            return query.OrderByDescending(x => x.CreatedOn);
        }
    }
}
